"""
用户相关数据模型
================

定义用户相关的请求和响应模型
"""

from datetime import datetime
from typing import List, Optional
from pydantic import BaseModel, EmailStr, Field


class UserResponse(BaseModel):
    """用户响应模型"""
    id: str = Field(..., description="用户ID")
    username: str = Field(..., description="用户名")
    email: str = Field(..., description="邮箱地址")
    is_active: bool = Field(..., description="是否激活")
    created_at: datetime = Field(..., description="创建时间")

    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": "123e4567-e89b-12d3-a456-426614174000",
                "username": "testuser",
                "email": "testuser@example.com",
                "is_active": True,
                "created_at": "2023-01-01T00:00:00Z"
            }
        }


class UserCreateRequest(BaseModel):
    """创建用户请求模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: EmailStr = Field(..., description="邮箱地址")
    password: str = Field(..., min_length=8, description="密码")
    is_active: bool = Field(default=True, description="是否激活")

    class Config:
        json_schema_extra = {
            "example": {
                "username": "newuser",
                "email": "newuser@example.com",
                "password": "securepassword123",
                "is_active": True
            }
        }


class UserUpdateRequest(BaseModel):
    """更新用户请求模型"""
    username: Optional[str] = Field(None, min_length=3, max_length=50, description="用户名")
    email: Optional[EmailStr] = Field(None, description="邮箱地址")
    is_active: Optional[bool] = Field(None, description="是否激活")

    class Config:
        json_schema_extra = {
            "example": {
                "username": "updateduser",
                "email": "updated@example.com",
                "is_active": True
            }
        }


class UserListResponse(BaseModel):
    """用户列表响应模型"""
    users: List[UserResponse] = Field(..., description="用户列表")
    total: int = Field(..., description="总数量")
    skip: int = Field(..., description="跳过的记录数")
    limit: int = Field(..., description="返回的记录数")

    class Config:
        json_schema_extra = {
            "example": {
                "users": [
                    {
                        "id": "123e4567-e89b-12d3-a456-426614174000",
                        "username": "user1",
                        "email": "user1@example.com",
                        "is_active": True,
                        "created_at": "2023-01-01T00:00:00Z"
                    }
                ],
                "total": 100,
                "skip": 0,
                "limit": 20
            }
        }


class UserProfileResponse(UserResponse):
    """用户个人资料响应模型（扩展版）"""
    full_name: Optional[str] = Field(None, description="全名")
    phone: Optional[str] = Field(None, description="电话")
    last_login: Optional[datetime] = Field(None, description="最后登录时间")
    profile_picture: Optional[str] = Field(None, description="头像URL")
    bio: Optional[str] = Field(None, description="个人简介")
    country: Optional[str] = Field(None, description="国家")
    city: Optional[str] = Field(None, description="城市")
    address: Optional[str] = Field(None, description="地址")

    class Config:
        from_attributes = True
        json_schema_extra = {
            "example": {
                "id": "123e4567-e89b-12d3-a456-426614174000",
                "username": "testuser",
                "email": "testuser@example.com",
                "is_active": True,
                "created_at": "2023-01-01T00:00:00Z",
                "full_name": "Test User",
                "phone": "+1234567890",
                "last_login": "2023-01-02T12:00:00Z",
                "profile_picture": "https://example.com/avatar.jpg",
                "bio": "This is my bio",
                "country": "USA",
                "city": "New York",
                "address": "123 Main St"
            }
        }


class UserProfileUpdateRequest(BaseModel):
    """更新用户个人资料请求模型"""
    username: Optional[str] = Field(None, min_length=3, max_length=50, description="用户名")
    email: Optional[EmailStr] = Field(None, description="邮箱地址")
    full_name: Optional[str] = Field(None, max_length=100, description="全名")
    phone: Optional[str] = Field(None, max_length=20, description="电话")
    profile_picture: Optional[str] = Field(None, description="头像URL")
    bio: Optional[str] = Field(None, max_length=500, description="个人简介")
    country: Optional[str] = Field(None, max_length=50, description="国家")
    city: Optional[str] = Field(None, max_length=50, description="城市")
    address: Optional[str] = Field(None, max_length=200, description="地址")

    class Config:
        json_schema_extra = {
            "example": {
                "username": "updateduser",
                "email": "updated@example.com",
                "full_name": "Updated User",
                "phone": "+9876543210",
                "profile_picture": "https://example.com/new-avatar.jpg",
                "bio": "Updated bio",
                "country": "Canada",
                "city": "Toronto",
                "address": "456 Elm St"
            }
        }
